<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">

<div id="app">

    <div class="content-header">
        <h1>系统设置
            <small>权限管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="权限名" style="width: 200px;" class="filter-item"
                          v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleAdd()">新建权限</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="list" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="权限名" align="center"></el-table-column>
                <el-table-column prop="keyword" label="权限关键词" align="center"></el-table-column>
                <el-table-column prop="description" label="权限描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">                         <!--弹出删除页面-->
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <div class="add-form">
                <el-dialog title="新建权限" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="permission" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名" prop="name">
                                    <el-input v-model="permission.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限键词" prop="keyword">
                                    <el-input v-model="permission.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="permission.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="addPermission()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <div class="edit-form">
                <el-dialog title="编辑权限" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="permission" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名" prop="name">
                                    <el-input v-model="permission.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限键词" prop="keyword">
                                    <el-input v-model="permission.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="permission.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="editPermission()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            list: [],//分页查询显示的列表
            permission: {},//新增,编辑页面的数据
            dialogFormVisible: false ,//添加功能的页面
            dialogFormVisible4Edit: false//编辑功能的页面
        },
        mounted() {
            this.findPage();
        },
        methods: {
            //分页查询
            findPage() {
               var params={
                   currentPage:this.pagination.currentPage,
                   pageSize: this.pagination.pageSize,
                   queryString: this.pagination.queryString
                };
               var url = "/permission/findPage";
               axios.get(url,{params:params}).then((response)=>{
                   if(response.data.flag){
                       this.list = response.data.data.rows;
                       this.pagination.total = response.data.data.total;
                       return;
                   }
                   this.$message.error(response.data.message);
               })
            },
            //跳转当前页面
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //弹出添加窗口
            handleAdd() {
                this.dialogFormVisible = true;
                this.permission = {};
            },
            //添加功能
            addPermission() {
                this.$refs['dataAddForm'].validate((valid)=>{
                    //如果校验通过
                    if(valid){
                        var url = "/permission/add";
                        axios.post(url,this.permission).then((response)=>{
                            if(response.data.flag){
                                this.dialogFormVisible = false;
                                this.$message.success(response.data.message);
                                return;
                            }
                            this.$message.error(response.data.message);
                        }).finally(()=>{
                            this.findPage();
                        })
                    }else{
                        this.$message('表单校验数据失败');
                        return false;
                    }
                })

            },
            //编辑功能
            editPermission(){
                //提交表单之前进行校验
                this.$refs['dataEditForm'].validate((valid)=>{
                    if(valid){
                        this.dialogFormVisible4Edit = false;
                        //表单校验通过,发送异步请求
                        var url = "/permission/edit";
                        axios.put(url,this.permission).then((response)=>{
                            if(response.data.flag){
                                this.$message.success(response.data.message);
                                return;
                            }
                            this.$message.error(response.data.message);
                        }).finally(()=>{
                            this.findPage();
                        })
                    }else{
                        this.$message.error('表单校验失败!');
                        return false;
                    }
                })

            },
            //删除功能
            handleDelete(row){
                var url = "/permission/delete?id="+row.id;
                this.$confirm("您确定要删除此项嘛?","提示",{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning'
                }).then(()=>{
                    //发送异步请求
                    axios.delete(url).then((response)=>{
                        if(response.data.flag){
                            this.$message.success(response.data.message);
                            return;
                        }
                        this.$message.error(response.data.message);
                    }).finally(()=>{
                        this.findPage();
                    })
                })
            },
            //弹出编辑窗口
            handleUpdate(row){
                this.dialogFormVisible4Edit = true;
                var url = "/permission/findById?id="+row.id;
                axios.get(url).then((response)=>{
                    if(response.data.flag){
                        this.permission = response.data.data;
                        return;
                    }
                    this.$message.error(response.data.message);
                })
            }
        }
    })
</script>
</html>